package cn.stylefeng.guns.modular.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.entity.GuideImg;
import cn.stylefeng.guns.modular.mapper.GuideImgMapper;
import cn.stylefeng.guns.modular.service.IGuideImgService;
import cn.stylefeng.guns.modular.util.IdWorker;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 启动页 引导页表 服务实现类
 * </p>
 *
 * @author XiangTianGe
 * @since 2020-04-15
 */
@Service
public class GuideImgServiceImpl extends ServiceImpl<GuideImgMapper, GuideImg> implements IGuideImgService {

    @Autowired
    private GuideImgMapper guideImgMapper;

    @Autowired
    private IdWorker idWorker;

    @Override

    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }

    @Override
    public void add(GuideImg guideImg) {
        guideImg.setCreatdAt(new Date());
        guideImg.setUpdatedAt(guideImg.getCreatdAt());
        guideImg.setId(idWorker.nextId());
        guideImgMapper.insert(guideImg);
    }

    @Override
    public void update(GuideImg guideImg) {
        guideImg.setUpdatedAt(new Date());
        guideImgMapper.updateById(guideImg);
    }

    @Override
    public void delete(Long id) {
        guideImgMapper.deleteById(id);
    }

    @Override
    public LayuiPageInfo getList(Integer type, Page page) {
        // 查询条件
        QueryWrapper qw = new QueryWrapper();
        qw.orderByDesc("updated_at");
        if (type != null) {
            qw.eq("type", type);
        }
        IPage iPage = guideImgMapper.selectPage(page, qw);
        return LayuiPageFactory.createPageInfo(iPage);
    }

    @Override
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        return modelAndView;
    }

    @Override
    public ModelAndView edit(Long id) {
        GuideImg guideImg = guideImgMapper.selectById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("guide", guideImg);
        return modelAndView;
    }
}
